<template>
  <!-- 封装三级联动组件 -->
  <!-- 商品分类导航 -->
  <div class="type-nav">
    <div class="container">
      <!-- 事件委派 -->
      <div @mouseleave="mouseleavechange" @mouseenter="allbtn">
        <h2 class="all">全部商品分类</h2>
        <!-- 过渡动画标签 -->
        <transition name="sort">
          <!-- 三级联动 -->
          <div class="sort" v-if="types">
            <!-- 利用事件委派 + 编程式导航 -->
            <div class="all-sort-list2" @click="goSearch">
              <!-- 一级分类 -->
              <div class="item bo" :class="{ bac: activeIndex === i }" v-for="(c1, i) in categoryLists" :key="c1.categoryId" @mouseenter="mouseenterchange(i)">
                <h3>
                  <a :data-categoryName="c1.categoryName" :data-category1Id="c1.categoryId">{{ c1.categoryName }}</a>
                </h3>
                <!-- 二级三级分类 -->
                <div class="item-list clearfix" :style="{ display: activeIndex === i ? 'block' : 'none' }">
                  <div class="subitem" v-for="c2 in c1.categoryChild" :key="c2.categoryId">
                    <dl class="fore">
                      <dt>
                        <a :data-categoryName="c2.categoryName" :data-category2Id="c2.categoryId">{{ c2.categoryName }}</a>
                      </dt>
                      <dd>
                        <em v-for="c3 in c2.categoryChild" :key="c3.categoryId">
                          <a :data-categoryName="c3.categoryName" :data-category3Id="c3.categoryId">{{ c3.categoryName }}</a>
                        </em>
                      </dd>
                    </dl>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </transition>
      </div>
      <nav class="nav">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">尚品汇超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import throttle from 'lodash/throttle'
export default {
  name: 'typeNav',
  computed: {
    ...mapState('home', ['categoryLists']),
  },
  data() {
    return {
      // 导航栏高亮的标识
      activeIndex: -1,
      // 控制导航栏是否显示标识
      types: false,
    }
  },
  mounted() {
    // 如果是主页则默认显示
    if (this.$route.path == '/home') {
      this.types = true
    }
  },
  methods: {
    // 移入三级联动
    mouseenterchange: throttle(function (e) {
      // console.log('触发了')
      this.activeIndex = e
    }, 10),
    // 移出三级联动
    mouseleavechange() {
      // 清消颜色
      this.activeIndex = -1
      // 针对search页面的全部商品按钮移出事件
      if (this.$route.path != '/home') {
        this.types = false
      }
    },
    // 针对search页面的全部商品按钮移入事件
    allbtn() {
      if (this.$route.path != '/home') {
        this.types = true
      }
    },
    // 委托
    goSearch(e) {
      // console.log(e.target)
      // console.log(e.target.dataset)
      // 解构自定义属性对象身上的值
      const { categoryname, category1id, category2id, category3id } = e.target.dataset
      // 确定是a标签(判断自定义属性)
      if (categoryname) {
        // 路由跳转对象
        let location = {
          name: 'search',
        }
        // 参数对象(a标签名字 + 分级id)
        let query = { categoryName: categoryname }
        if (category1id) {
          // 动态添加
          query.category1Id = category1id
        } else if (category2id) {
          // 动态添加
          query.category2Id = category2id
        } else {
          // 动态添加
          query.category3Id = category3id
        }
        // 参数的合并(有则合并)
        if (this.$route.params) {
          // 整理params参数
          location.params = this.$route.params
          // 整理参数(合并对象)
          location.query = query
          // console.log(query)
          // 带领2个参数跳转
          this.$router.push(location)
        }
      }
    },
  },
}
</script>

<style lang="less" scoped>
a {
  cursor: pointer;
}
.bac {
  background-color: skyblue;
}
.type-nav {
  border-bottom: 2px solid #e1251b;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
      cursor: pointer;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      position: absolute;
      background: #fafafa;
      z-index: 999;

      .all-sort-list2 {
        .item {
          h3 {
            line-height: 30px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;

            a {
              color: #333;
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }

          // &:hover {
          //   .item-list {
          //     display: block;
          //   }
          // }
        }
      }
    }
    // 过渡动画开始
    .sort-enter {
      height: 0;
    }
    // 过渡动画开始(结束)
    .sort-enter-to {
      height: 461px;
    }
    // 定义动画进入时间,规律
    .sort-enter-active {
      transition: all 0.5s linear;
      overflow: hidden;
    }
    // 过渡动画离开
    .sort-leave {
      height: 461px;
    }
    // 过渡动画离开(结束)
    .sort-leave-to {
      height: 0;
    }
    // 定义动画离开时间,规律
    .sort-leave-active {
      transition: all 0.5s linear;
      overflow: hidden;
    }
  }
}
</style>
